﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using MySql.Data.MySqlClient;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TravellerApp1.Views
{
    /// <summary>
    /// AdminWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AdminWindow : Window
    {
        public AdminWindow()
        {
            InitializeComponent();
            this.Title = "管理员端（" + MysqlHelper.GetConnectionString() + "）";
            table_type.ItemsSource = TableTypeWrappers.All;
            table_type.SelectedValuePath = "TableType";
            table_type.DisplayMemberPath = "Description";
        }

        public enum TableType
        {
            FLIGHTS,
            HOTELS,
            BUS,
            CUSTOMERS
        }

        private Dictionary<string, string> PrimaryKeyDictionary = new()
        {
            { "FLIGHTS", "flightNum" },
            { "HOTELS", "location" },
            { "BUS", "location" },
            { "CUSTOMERS", "custName" }
        };

        public class TableTypeWrapper
        {
            public TableType TableType { get; }
            public string? Description { get; }
            public TableTypeWrapper(TableType type, string description)
            {
                this.TableType = type;
                this.Description = description;
            }
        }

        public static class TableTypeWrappers
        {
            public static IEnumerable<TableTypeWrapper> All =>
                new List<TableTypeWrapper>
                {
                    new TableTypeWrapper(TableType.FLIGHTS, "航班"),
                    new TableTypeWrapper(TableType.HOTELS, "宾馆房间"),
                    new TableTypeWrapper(TableType.BUS, "大巴车"),
                    new TableTypeWrapper(TableType.CUSTOMERS, "客户")
                };
        }

        private void btn_add_flight_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string cmd1 = "insert into flights values('" + tb_flight1.Text + "'," + tb_flight2.Text + ","
                    + tb_flight3.Text + "," + tb_flight4.Text + ",'" + tb_flight5.Text + "','" + tb_flight6.Text + "');";
                MysqlHelper.ExecuteSqlScript(cmd1, 2);
                MessageBox.Show("添加成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MysqlHelper.SetDatabase("travel947");
            }
            finally
            {
                MysqlHelper.CloseCon();
            }
        }

        private void btn_add_bus_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string cmd1 = "insert into bus values('" + tb_bus1.Text + "'," + tb_bus2.Text + ","
                    + tb_bus3.Text + "," + tb_bus4.Text + ");";
                //MessageBox.Show(cmd1);
                MysqlHelper.ExecuteSqlScript(cmd1, 2);
                MessageBox.Show("添加成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MysqlHelper.SetDatabase("travel947");
            }
            finally
            {
                MysqlHelper.CloseCon();
            }
        }

        private void btn_add_hotel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string cmd1 = "insert into hotels values('" + tb_hotel1.Text + "'," + tb_hotel2.Text + ","
                    + tb_hotel3.Text + "," + tb_hotel4.Text + ");";
                //MessageBox.Show(cmd1);
                MysqlHelper.ExecuteSqlScript(cmd1, 2);
                MessageBox.Show("添加成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MysqlHelper.SetDatabase("travel947");
            }
            finally
            {
                MysqlHelper.CloseCon();
            }
        }

        private void btn_add_customer_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string cmd1 = "insert into customers values('" + tb_customer1.Text + "','" 
                    + tb_customer2.Text + "','" + tb_customer3.Text + "');";
                MysqlHelper.ExecuteSqlScript(cmd1, 2);
                MessageBox.Show("添加成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MysqlHelper.SetDatabase("travel947");
            }
            finally
            {
                MysqlHelper.CloseCon();
            }
        }

        private void btn_delete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string tablename = table_type.SelectedValue.ToString();
                string pkey = PrimaryKeyDictionary[tablename];
                string pkeycontent = tb_delete.Text;
                string cmd1 = "delete from " + tablename + " where " + pkey + "='" + pkeycontent + "';";
                MysqlHelper.ExecuteSqlScript(cmd1, 2);
                MessageBox.Show("删除成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MysqlHelper.SetDatabase("travel947");
            }
            finally
            {
                MysqlHelper.CloseCon();
            }
        }

        private void btn_exec_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MysqlHelper.SetDatabase("travel947");
                string cmd1 = tb_sql.Text;
                MysqlHelper.ExecuteSqlScript(cmd1, 2);
                MessageBox.Show("执行成功");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                MysqlHelper.SetDatabase("travel947");
            }
            finally
            {
                MysqlHelper.CloseCon();
            }
        }

        private void enter_query_Click(object sender, RoutedEventArgs e)
        {
            GotoQueryWindow(this);
        }

        public static void GotoQueryWindow(AdminWindow current)
        {
            QueryWindow login1 = new();
            login1.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            login1.Show();
            WinHelper.ForceClose(current);
        }

        private void enter_query1_Click(object sender, RoutedEventArgs e)
        {
            GotoRoute(this);
        }

        public static void GotoRoute(AdminWindow current)
        {
            Route login1 = new();
            login1.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            login1.Show();
            WinHelper.ForceClose(current);
        }
    }
}
